package com.newland.rbac.utils;

import com.newland.rbac.vo.MenuVo;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class MenuUtil {
    public static List<MenuVo> buildMenuTree(List<MenuVo> menus){
        List<MenuVo> fathers = menus.stream().filter(menuVo -> menuVo.getPid() == 0).collect(Collectors.toList());
        List<MenuVo> sons = menus.stream().filter(menuVo -> menuVo.getPid() != 0).collect(Collectors.toList());
        for (MenuVo father : fathers) {
            ArrayList<MenuVo> list = new ArrayList<>();
            for (MenuVo son : sons) {
                if(son.getPid()==father.getId()){
                    list.add(son);
                }
            }
            father.setChildren(list);
        }
        return fathers;
    }
}
